<?php
global $_RQ,$_AR;
$_RQ['op']=$_RQ['op']?$_RQ['op']:'list';
if($_RQ['op']=='list'){
	if($return['pluginset']['is_guoshen'] && $return['pluginset']['guoshen_url'] && in_array($_SESSION['container'],array('baiduapp','aliapp','ttapp','wxapp'))){
		exi('','',$return['pluginset']['guoshen_url']);	
	}
	$city=$this->model('city')->gocity($_RQ);
	$return['member']=cfc('core')->get_member($_SESSION['uid']);
	$params=array('uniacid'=>$_SESSION['uniacid']);
	$pindex = max(1, intval($_SESSION['page']));
	$psize = 15;
	$total='';
	$return['merchgroups']=pdo_getslice('jy_merch_category',$params,array($pindex,$psize),$total,$fields,'','id desc');
	$return['basic']['searchkeyword']=$return['basic']['searchkeyword']?explode('|',$return['basic']['searchkeyword']):array();
	$return['list']=array();
	$merch_types=$this->merch_types2;
	$return['merch_types']=array();
	foreach($merch_types as $k=>&$mv){
		$childs=pdo_getall('jy_merch_category',array('type'=>$k,'uniacid'=>$_SESSION['uniacid']));
		$return['merch_types'][]=array(
				'id'=>$k,
				'title'=>$mv,
				'childs'=>$childs,
		);
	}
	$return['params']=array('order'=>'fanfee','cid'=>intval($_RQ['cid']));
	$return['pcd']=$city['pcd'];
	$return['city']=$city;
	$return['redset']=$this->plugin_setting('redset');
	//获取当前时间：
	$showtypetime1=mktime(10,30,0,date('m'),date('d'),date('Y'));
	$showtypetime2=mktime(14,00,0,date('m'),date('d'),date('Y'));
	$showtypetime3=mktime(17,00,0,date('m'),date('d'),date('Y'));
	$showtypetime4=mktime(21,00,0,date('m'),date('d'),date('Y'));
	if(TIMESTAMP<$showtypetime1){
		$return['showtype']=1;
	}elseif(TIMESTAMP>$showtypetime1 && TIMESTAMP<$showtypetime2){
		$return['showtype']=2;
	}elseif(TIMESTAMP>$showtypetime2 && TIMESTAMP<$showtypetime3){
		$return['showtype']=3;
	}elseif(TIMESTAMP>$showtypetime3 && TIMESTAMP<$showtypetime4){
		$return['showtype']=4;
	}elseif(TIMESTAMP>$showtypetime4){
		$return['showtype']=5;
	}
	foreach($return['pluginset']['hdps'] as &$hv){
		$hv['picurl']=tomedia($hv['picurl']);
		$hv['video']=tomedia($hv['video']);
	}
	foreach($return['pluginset']['bankuai'] as &$bv){
		$bv['img']=tomedia($bv['img']);
	}
	$return['pluginset']['indexad']=tomedia($return['pluginset']['indexad']);
	$return['showdaytypetitle']=$this->showdaytype[$return['showtype']];
	$sharelink='jy_newpaimerch/index//cityid='.$_SESSION['cityid'].'&fromuser='.$_SESSION['uid'];
	$return['sharedata']=array(
			'title'=>$return['pluginset']['share_title']?$return['pluginset']['share_title']:'首页-消费联盟',
			'desc'=>$return['pluginset']['share_desc']?$return['pluginset']['share_desc']:'消费联盟，一个属于自己的城市联盟！',
			'link'=>custom_url($sharelink,true),
			'imgUrl'=>tomedia($return['pluginset']['share_image'])
	);
	set_html('title',$return['pluginset']['title']?:'消费联盟');
	exi($return);
}
if($_RQ['op']=='getgoods'){
	$showtype=intval($_RQ['showtype']);
	$bankuai_id=intval($_RQ['bankuai_id']);
	if(!$_RQ['cid']){
		if($showtype){
			foreach($return['pluginset']['bankuai'] as $bv){
				if($showtype && $bv['showtype']==$showtype){
					$nowmerchgroups=str_array($bv['merchgroups']);
				}
				if(!$bv['showtype']){
					$nowmerchgroups=str_array($bv['merchgroups']);
				}
				$merchgroups=$merchgroups?:$nowmerchgroups;
				if($merchgroups){
					$merchgroups=array_merge($merchgroups,$nowmerchgroups);
				}else{
					$merchgroups=$nowmerchgroups;
				}
			}
		}else{
			$merchgroups=$return['pluginset']['bankuai'][$bankuai_id]['merchgroups'];
			$merchgroups=str_array($merchgroups);
		}
		if(empty($merchgroups)){
			exi(array('list'=>array()));
		}
	}else{
		$merchgroups=intval($_RQ['cid']);
		$return['params']['cid']=intval($_RQ['cid']);
	}
	//获取最近50家店
	if($_RQ['lat'] || $_RQ['lng']){
		$lat_lng=array('lat'=>$_RQ['lat'],'lng'=>$_RQ['lng']);
		$this->session('lat_lng',$lat_lng);
	}
	$lat_lng=$this->session('lat_lng');
	$params=array(
		'uniacid'=>$_SESSION['uniacid'],
		'status'=>1,
		'cateid'=>$merchgroups,
	);
	if(!$_RQ['city'] || $return['pluginset']['is_onecity']){
		$_RQ['city']=pdo_getcolumn('jy_newpaimerch_city',array('id'=>$_SESSION['cityid'],'uniacid'=>$_SESSION['uniacid']),'title');
	}
	$params['pcd like']='%'.$_RQ['city'].'%';
	$return['params']['iscity']=$_RQ['iscity'];
	$return['params']['city']=$_RQ['city'];
	$pindex = max(1, 1);
	$parameter=array(
		'params'=>$params,
		'limit'=>array($pindex,10000),//获取所有
		'fields'=>array('id','merchname','lat_lng','logo','cateid'),
		'key'=>'id',
		'orderby'=>$orderby,
	);
	$merchs=model('jy_merch/model')->getmerch($parameter,6,$lat_lng,intval($_RQ['page']));
	$merchids=array();
	foreach($merchs as &$v){
			$v['onlinetime']=json_decode($v['onlinetime'],true);
			if($v['juli']<10){
				$v['peisongtime']=$v['juli']*8+28;
			}else if($v['juli']<30){
				$v['peisongtime']=$v['juli']*3+28;
			}else{
				$v['peisongtime']=$v['juli']*1+28;
			}
			$v['out_juli']=$v['out_juli']>0?$v['out_juli']:0;
			$merchids[]=$v['id'];
	}
	if(count($merchs)==0){
		exi(array('list'=>array()));
	}
	//当前时间段热门商品
	$params=array(
		'uniacid'=>$_SESSION['uniacid'],
		'status'=>1,
		'nochecked'=>0,
		'merchid'=>$merchids,
	);
	$pindex = max(1, intval($_RQ['page']));
	$psize = 10;
	if($_RQ['num']){
		$psize=$_RQ['num'];
	}
	$total='';
	$fields=array('id','merchid','title','image','price','active_plugin','active_id','salenum','originalprice','usermaxbuy','unit');
	$orderby='salerealnum desc ';
	if($_RQ['order']){
		if($_RQ['order']=='sales'){
			$orderby='salerealnum desc';
		}
		if($_RQ['order']=='price'){
			if($_RQ['order_type']==2){
				$orderby='price desc';
			}else{
				$orderby='price asc';
			}
			
		}
	}
	$parameter=array(
		'params'=>$params,
		'limit'=>array($pindex,$psize),
		'fields'=>$fields,
		'orderby'=>$orderby,
	);
	$list=model('jy_weishop/goods')->goods_list($parameter,$total);
	foreach($list as &$itemv){
		$itemv['originalprice']=$itemv['originalprice']?$itemv['originalprice']:price($itemv['price']*1.2);
		//按点赞数排序作品
		$pindex = max(1, 0);
		$members=pdo_getslice('jy_weishop_order_goods',array('uniacid'=>$_SESSION['uniacid'],'goodsid'=>$itemv['id']),array($pindex,5),$total,array('mid'),'','id desc');
		foreach($members as &$mv){
			$memberinfo=pdo_get('core_members',array('id'=>$mv['mid']),array('nickname','avatar'));
			$mv['nickname']=$memberinfo['nickname'];
			$mv['avatar']=$memberinfo['avatar'];
		}
		$itemv['members']=$members;
		$install = check_permission('jy_merch');
		if($install){
			$itemv['merchname']=pdo_getcolumn('jy_merch_user',array('id'=>$itemv['merchid']),'merchname');
		}
		$itemv['likes']=pdo_count('jy_weishop_member_history',array('uniacid'=>$_SESSION['uniacid'],'goodsid'=>$itemv['id']),'id');
		$itemv['image']=$itemv['image']?tomedia($itemv['image']):'';
	}
	$return['list']=array_values($list);
	exi($return);
}
if($_RQ['op']=='getmerchs'){
	$showtype=intval($_RQ['showtype']);
	$bankuai_id=intval($_RQ['bankuai_id']);
	$islike=intval($_RQ['islike']);
	if(!$_RQ['cid'] && ($showtype || $bankuai_id)){
		if($showtype){
			foreach($return['pluginset']['bankuai'] as $bv){
				if($showtype && $bv['showtype']==$showtype){
					$nowmerchgroups=str_array($bv['merchgroups']);
				}
				if(!$bv['showtype']){
					$nowmerchgroups=str_array($bv['merchgroups']);
				}
				$merchgroups=$merchgroups?:$nowmerchgroups;
				if($merchgroups){
					$merchgroups=array_merge($merchgroups,$nowmerchgroups);
				}else{
					$merchgroups=$nowmerchgroups;
				}
			}
		}else{
			$merchgroups=$return['pluginset']['bankuai'][$bankuai_id]['merchgroups'];
			$merchgroups=str_array($merchgroups);
		}
		if(count($merchgroups)==0){
			//exi(array('list'=>array()));
		}
	}elseif($_RQ['cid']){
		$merchgroups=intval($_RQ['cid']);
		$return['params']['cid']=intval($_RQ['cid']);
	}
	//获取最近50家店
	if($_RQ['lat'] || $_RQ['lng']){
		$lat_lng=array('lat'=>$_RQ['lat'],'lng'=>$_RQ['lng']);
		$this->session('lat_lng',$lat_lng);
	}
	$lat_lng=$this->session('lat_lng');
	$params=array(
		'uniacid'=>$_SESSION['uniacid'],
		'status'=>1,
		'merchname <>'=>'',
		'lat_lng <>'=>'',
	);
	if($merchgroups){
		$params['cateid']=$merchgroups;
	}
	if(!$_RQ['city'] && $return['pluginset']['is_onecity']){
		$_RQ['city']=pdo_getcolumn('jy_newpaimerch_city',array('id'=>$_SESSION['cityid'],'uniacid'=>$_SESSION['uniacid']),'title');
	}
	if($_RQ['city'] && $return['pluginset']['is_onecity']){
		$params['pcd like']='%'.$_RQ['city'].'%';
		$return['params']['city']=$_RQ['city'];
	}
	$return['params']['iscity']=$_RQ['iscity'];
	$orderby='isrecommand desc, star desc, salenum desc, id desc';
	//$orderbykey='isrecommand';
	if(trim($_RQ['order']) && trim($_RQ['order'])!='all'){
		$order=trim($_RQ['order']);
		if($order=='salenum' || $order=='sales'){
			$orderby='salenum desc, isrecommand desc, id desc';
			$orderbykey='salenum';
		}
		if($order=='sudu'){
			$orderby='sudu desc, isrecommand desc, salenum desc, id desc';
			$orderbykey='sudu';
		}
		if($order=='star'){
			$orderby='star desc, isrecommand desc, salenum desc, id desc';
			$orderbykey='star';
		}
		if($order=='brand'){
			$params['isbrand']=1;
		}
		if($order=='you'){
			$orderby='isrecommand desc, salenum desc, id desc';
			$orderbykey='isrecommand';
		}
		if($order=='out_pay'){
			$orderby='out_pay desc, isrecommand desc, salenum desc, id desc';
			$orderbykey='out_pay';
		}
		if($order=='out_price'){
			$orderby='out_price desc, isrecommand desc, salenum desc, id desc';
			$orderbykey='out_price';
		}
		if($order=='istuijie'){
			$orderby='salenum desc, id desc';
			$orderbykey='istuijie';
		}
		$return['params']['order']=trim($_RQ['order']);
	}
	if(intval($_RQ['star'])){
		$params['star >']=intval($_RQ['star'])-0.2;
		$return['params']['star']=intval($_RQ['star']);
	}
	if(trim($_RQ['keyword'])){
		$idsparams=$params;
		$idsparams['merchname like']='%'.trim($_RQ['keyword']).'%';
		$return['params']['keyword']=trim($_RQ['keyword']);
		$merchids=pdo_getall('jy_merch_user',$idsparams,'id','id');
		if($merchids){
			$merchids=array_keys($merchids);
			$params['id']=$merchids;
		}else{
			$goodsparams=array(
				'uniacid'=>$_SESSION['uniacid'],
				'status'=>1,
				'merchid >'=>0,
			);
			$goodsparams['title like']='%'.trim($_RQ['keyword']).'%';
			$merchids=pdo_getall('jy_weishop_goods',$goodsparams,'merchid','merchid');
			if($merchids){
				$merchids=array_keys($merchids);
				$params['id']=$merchids;
			}
		}
	}
	if($order=='istuijie'){
		$idsparams=$params;
		$idsparams['isrecommand']=1;
		$merchids=pdo_getall('jy_merch_user',$idsparams,'id','id');
		if($merchids){
			$merchids=array_keys($merchids);
			$params['id']=$merchids;
		}else{
			$params['id']=0;
		}
	}
	if($islike==1){
		$merchids=pdo_getall('jy_merch_like_log',array('uniacid'=>$_SESSION['uniacid'],'mid'=>$_SESSION['uid'],'islike'=>1),'merchid','merchid');
		if($merchids){
			$merchids=array_keys($merchids);
			$params['id']=$merchids;
		}else{
			$params['id']=-1;
		}
	}
	$pindex = max(1, 1);
	//$params['isonlinetime']=1;
	//print_r($params);exit;
	$parameter=array(
		'params'=>$params,
		'limit'=>array($pindex,10000),//获取所有
		'fields'=>array('id','star','salenum','type','offline','isrecommand','merchname','out_price','out_pay','out_juli','lat_lng','logo','topimg','cateid','onlinetime','address','setting'),
		'key'=>'id',
		'orderby'=>$orderby,
		'orderbykey'=>$orderbykey,
		'no_offline'=>$return['pluginset']['no_offline'],
	);
	if($order=='fanfee'){
		$parameter['order']='fanfee';
	}
	$parameter['pai_feerate']=$this->setting['pai_feerate'];
	$merchs=model('jy_merch/model')->getmerch($parameter,$_RQ['limit'],$lat_lng,intval($_RQ['page']),intval($_RQ['juli']));
	$citypeisong=plugin('jy_weishop')->plugin_setting('citypeisong');
	$days=date('Y-m-d',TIMESTAMP);
	foreach($merchs as $k=>&$v){
			$v['logo']=$v['logo']?tomedia($v['logo']):'';
			$v['topimg']=$v['topimg']?tomedia($v['topimg']):'';
			if($v['juli']<10){
				$v['peisongtime']=$v['juli']*5+28;
			}else if($v['juli']<30){
				$v['peisongtime']=$v['juli']*3+28;
			}else{
				$v['peisongtime']=$v['juli']*1+28;
			}
			$v['out_juli']=$v['out_juli']>0?$v['out_juli']:8;
			$v['out_price']=$v['out_price']>0?$v['out_price']:$citypeisong['out_price'];
			if($_RQ['getgoods'] || $order=='istuijie'){
				$pindex = max(1, 1);
				$psize = 3;
				$params=array('uniacid'=>$_SESSION['uniacid'],'status'=>1,'nochecked'=>0,'merchid'=>$v['id']);
				$v['goodsnum']=pdo_count('jy_weishop_goods',$params);
				$fields=array('id','title','price','image','unit','originalprice','nochecked');
				$goods=pdo_getslice('jy_weishop_goods',$params,array($pindex,$psize),$total,$fields,'','salenum desc, id desc');
				foreach($goods as &$gv){
					$gv['image']=tomedia($gv['image']);
					$gv['originalprice']=$gv['originalprice']?:$gv['price']*1.2;
					$gv['originalprice']=price($gv['originalprice']);
					$v['tags']=pdo_getcolumn('jy_weishop_goods_comments',array('uniacid'=>$_SESSION['uniacid'],'goodsid'=>$gv['id']),'tags');
				}
				$v['goods']=$goods;
			}
			$v['coupons']=model('jy_weishop/coupon')->getmerchcoupon($v['id'],1);
			$salenum=pdo_getsum('jy_weishop_order_goods',array('uniacid'=>$_SESSION['uniacid'],'merchid'=>$v['id']),'total');
			if($salenum!=$v['salenum']){
                $v['salenum']=$salenum;
                pdo_update('jy_merch_user',array('salenum'=>$salenum),array('uniacid'=>$_SESSION['uniacid'],'id'=>$v['id']));
            }
			$goodsids=pdo_getall('jy_weishop_goods',array('merchid'=>$v['id']),'id','id');
			if($goodsids){
				$goodsids=array_keys($goodsids);
				$v['tags']=pdo_getcolumn('jy_weishop_goods_comments',array('uniacid'=>$_SESSION['uniacid'],'goodsid'=>$goodsids),'tags');
			}
			$tags=str_array($return['pluginset']['tags'],'|');
			$key=mt_rand(0,count($tags)-1);
			$v['tags']=$v['tags']?:$tags[$key];	
			$v['star']=$v['star']>0?$v['star']:'5.00';
	}
	$return['list']=array_values($merchs);
	exi($return);
}
if($_RQ['op']=='gored'){
	$pcd=$_SESSION['pcd'];
	$red=$this->model('red')->gored($pcd['city']);
	exi($red);
}
if($_RQ['op']=='getred'){
	$jiangitem=$this->model('red')->getred('',$_RQ['sendredlogid']);
	$jiangitem['typetitle']=$this->jiangtype[$jiangitem['type']];
	if($jiangitem['type']<=2){
		exi('恭喜抽中【'.$jiangitem['typetitle'].'】'.$jiangitem['fee'].'个！');
	}else{
		exi('恭喜抽中一张【'.$jiangitem['typetitle'].'】！');
	}
}
if($_RQ['op']=='uppcd'){
	$post=get_postdata();
	if($post['lat'] || $post['lng']){
		$lat_lng=array('lat'=>$post['lat'],'lng'=>$post['lng']);
		$this->session('lat_lng',$lat_lng);
	}
	if($post['city']){
		$pcd=array(
			'province'=>$post['province'],
			'city'=>$post['city'],
			'district'=>$post['district'],
		);
		$this->model('city')->upcity($post['city'],$pcd);
	}
	exi('更新成功！');
}
?>